package com.meng.mmlive.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;

import com.lqr.adapter.LQRViewHolder;
import com.lqr.adapter.OnItemClickListener;
import com.lqr.recyclerview.LQRRecyclerView;
import com.meng.baselibrary.fragment.BaseFragment;
import com.meng.mmlive.R;
import com.meng.mmlive.ui.activity.SessionActivity;
import com.meng.mmlive.ui.adapter.RecentConversationAdapter;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.message.TextMessage;

/**
 * Created by Administrator on 2017/5/15.
 */

public class RecentConversationListFragment extends BaseFragment implements OnItemClickListener {
    LQRRecyclerView recyclerView;
    private RecentConversationAdapter conversationAdapter;

    @Override
    protected int getContentViewLayout() {
        return R.layout.fragment_subconversation_list;
    }

    @Override
    protected void initView() {
        initPaddingTob();
        recyclerView = findViewById(R.id.recycleView);
    }

    @Override
    protected void initData() {
        initConversationList();
    }

    @Override
    public void onResume() {
        super.onResume();
       initData();
    }

    private List<Conversation> mData = new ArrayList<>();
    private void initConversationList() {
        RongIMClient.getInstance().getConversationList(new RongIMClient.ResultCallback<List<Conversation>>() {
            @Override
            public void onSuccess(List<Conversation> conversations) {
                if (conversations != null) {
                    mData.clear();
                    mData.addAll(conversations);
                    if(conversationAdapter == null){
                        conversationAdapter = new RecentConversationAdapter(context,mData,R.layout.item_recent_message);
                        conversationAdapter.setOnItemClickListener(RecentConversationListFragment.this);
                        recyclerView.setAdapter(conversationAdapter);
                    }else{
                        conversationAdapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
            }
        });
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMessageEvent(TextMessage textMessage) {
        initConversationList();
    };

    @Override
    public void onItemClick(LQRViewHolder helper, ViewGroup parent, View itemView, int position) {
        Intent intent = new Intent(context, SessionActivity.class);
        Conversation item = mData.get(position);
        intent.putExtra("sessionId", item.getTargetId());
        if (item.getConversationType() == Conversation.ConversationType.PRIVATE) {
            intent.putExtra("sessionType", SessionActivity.SESSION_TYPE_PRIVATE);
        } else {
            intent.putExtra("sessionType", SessionActivity.SESSION_TYPE_GROUP);
        }
        startActivity(intent);
    }


}
